import { createRouter, createWebHashHistory } from "vue-router";
import { Layout, getParentLayout } from "@/utils/router-helper";
import { useI18n } from "@/hooks/web/useI18n";
import { NO_RESET_WHITE_LIST } from "@/constants";

const { t } = useI18n();

export const constantRouterMap = [
  {
    path: "/",
    component: Layout,
    redirect: "/myApi",
    name: "Root",
    meta: {
      // hidden: true
      "title": "我的API",
      "icon": "vi-ant-design:home-outlined",
    },
    children: [
      {
        path: "/myApi", // ✅ 改这里
        component: () => import("@/views/MyApi/index.vue"),
        name: "MyApi",
        meta: {
          hidden: true,
          title: "我的API",
          noCache: true,
          affix: true,
          icon: "vi-ant-design:dashboard-outlined",
        },
      },
    ],
  },
  {
    path: "/redirect",
    component: Layout,
    name: "RedirectWrap",
    children: [
      {
        path: "/redirect/:path(.*)",
        name: "Redirect",
        component: () => import("@/views/Redirect/Redirect.vue"),
        meta: {},
      },
    ],
    meta: {
      hidden: true,
      noTagsView: true,
    },
  },
  {
    path: "/login",
    component: () => import("@/views/Login/Login.vue"),
    name: "Login",
    meta: {
      hidden: true,
      title: t("router.login"),
      noTagsView: true,
    },
  },
  {
    path: "/personal",
    component: Layout,
    redirect: "/personal/personal-center",
    name: "Personal",
    meta: {
      title: t("router.personal"),
      hidden: true,
      canTo: true,
    },
    children: [
      {
        path: "personal-center",
        component: () => import("@/views/Personal/PersonalCenter/index.vue"),
        name: "PersonalCenter",
        meta: {
          title: t("router.personalCenter"),
          hidden: true,
          canTo: true,
        },
      },
    ],
  },

  {
    path: "/404",
    component: () => import("@/views/Error/404.vue"),
    name: "NoFind",
    meta: {
      hidden: true,
      title: "404",
      noTagsView: true,
    },
  },
];

// 动态路由
export const asyncRouterMap = [];

//路由
const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});
//重置路由
export const resetRouter = () => {
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name && !NO_RESET_WHITE_LIST.includes(name)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
};

export const setupRouter = (app) => {
  app.use(router);
};

export default router;
